package com.baizhi.hospital.service.impl;

import com.baizhi.hospital.dao.DrugDao;
import com.baizhi.hospital.entity.Drug;
import com.baizhi.hospital.service.DrugService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Service
public class DrugServiceImpl implements DrugService {
	@Resource
	private DrugDao drugDao;

	@Override
	public Map<String, Object> selectDrugPage(int page, int limit, String drugName) {
		if(drugName!=null && !"".equals(drugName)){
			drugName = "%"+drugName+"%";
		}else{
			drugName = null;
		}
		int offset = (page-1)*limit;

		List<Drug> drugs = drugDao.selectPageAndsearch(offset, limit, drugName);
		int count = drugDao.selectCount(drugName);
//		将查到的数据放入map集合中
		Map<String,Object> map = new HashMap<>();
		map.put("data",drugs);
		map.put("count",count);
		map.put("code",0);
		map.put("msg","");
		return map;
	}

	@Override
	public Map<String, Object> insertDrug(Drug drug) {
		String drugId = UUID.randomUUID().toString().replace("-", "");
		drug.setDrugId(drugId);
		HashMap<String, Object> map = new HashMap<>();
		int i = drugDao.insertDrug(drug);
		if (i > 0) {
			map.put("isInsert","添加成功");
		}else{
			map.put("isInsert","添加失败");
		}
		return map;
	}

	@Override
	public Map<String, Object> deleteByDrugId(String drugId) {
		HashMap<String, Object> map = new HashMap<>();
		int i =  drugDao.deleteByDrugId(drugId);
		if(i > 0){
			map.put("isDelete","删除成功");
		}else{
			map.put("isDelete","删除失败");
		}
		return map;
	}

	@Override
	public Map<String, Object> updateDrugSaleStatus(String drugId, int drugSaleStatus) {
		HashMap<String, Object> map = new HashMap<>();
		int i = drugDao.updateDrugSaleStatus(drugId,drugSaleStatus);
		if(i > 0){
			map.put("isSales","操作成功");
		}else{
			map.put("isSales","操作失败");
		}
		return map;
	}

	@Override
	public Map<String, Object> updateDrug(Drug drug) {
		HashMap<String, Object> map = new HashMap<>();
		int i = drugDao.updateDrug(drug);
		if(i > 0){
			map.put("isUpdate","修改成功");
		}else{
			map.put("isUpdate","修改失败");
		}
		return map;
	}
}
